window.addEvent('domready', function() {
	$$('.ajax-forPages').addEvent('click',function(e){
		e.stop();
		var self = $(this);
		
		switch(self.retrieve('toggle')) {
			case null:
				var tr = self.getParent('tr');
				var tdCount = tr.getChildren('td').length;
				var ul = new Element('ol',{'class':'bottom'});
				var jsonRequest = new Request.JSON({
					url: this,
					async: false,
					onComplete: function(json){
					json.each(function(item){
						ul.adopt(new Element('li',{}).grab(new Element('a',{
							'text':item.url,
							'href':'/page/block/edit/id/'+item.id
						})));
					});
				}}).send();
				
				var toggleElement = new Element('tr',{'class':'toggle'}).grab(new Element('td',{'colspan':tdCount}).grab(ul));
				toggleElement.setStyle('display','block');
				toggleElement.inject(tr,'after');
				self.store('toggle',true);
				break;

			case false:
				self.store('toggle',true);
				self.getParent('tr').getNext('.toggle').setStyle('display','block');
				break;

			case true:
				self.store('toggle',false);
				self.getParent('tr').getNext('.toggle').setStyle('display','none');
				break;
		}
	});
});